// pages/otherManage/otherManage.js
import Dialog from '@vant/weapp/dialog/dialog';
const http = require("../../api/camp")
const api = require("../../api/index")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    campId: "",
    campConditionDatas: [], //营位数据列表
    scenicDatas: [], //景点数据列表
    activityDatas: [], //活动数据列表
    projectDatas: [], //项目数据列表
    campConditionList: [], //营位数据列表
    scenicList: [], //景点数据列表
    activityList: [], //活动数据列表
    projectList: [], //项目数据列表
  },
  onChange(event) {
    wx.showToast({
      title: `切换到标签 ${event.detail.title}`,
      icon: 'none',
    });
  },
  // 景点
  addScenic() {
    // console.log(123);
    this.setData({
      campScenicStatus: true,
    })
  },
  // 获取周边景点
  getScenicInfo() {
    api.getSpots(this.data.campId).then(res => {
      if (res.code == 200 && res.data) {
        this.setData({
          scenicList: res.data
        })
      }
      // console.log(res, "景点列表");
    })
  },
  //景点数据
  scenicList(e) {
    this.setData({
      scenicDatas: e.detail,
      // touristConditionData: e.detail
    })
    console.log(e);
    const scenic = this.data.scenicDatas
    const index = scenic.length - 1
    // console.log(scenic[index]);
    const object = scenic[index]
    var isEmpty = true;
    Object.keys(object).forEach(function (x) {
      if (object[x] === null || object[x] === "") {
        isEmpty = false;
      }
    });
    if (isEmpty) {
      // 添加请求
      http.addScenic({
        ...scenic[index],
        campid: this.data.campId
      }).then((res) => {
        console.log(res);
        if (res.code === 200) {
          wx.showToast({
            title: '提交成功，待审核',
            icon: 'none',
            duration: 1500
          })
          this.getScenicInfo()
        }
      })
    }

  },
  // 删除景点
  delScenic(event) {
    Dialog.confirm({
        title: '周边景点删除',
        message: '确定删除该景点吗？',
      })
      .then(() => {
        http.delScenic(event.currentTarget.dataset.id).then(res => {
          if (res.code == 200) {
            this.getScenicInfo()
          }
        })
      })
      .catch(() => {
        // on cancel
      });

  },
  // 获取营位
  getCampCampsite() {
    const query = {
      campid: this.data.campId
    }
    api.getCampCampsite(query).then(res => {
      console.log(res)
      if (res.code == 200 && res.data) {
        for(let i = 0; i<res.data.length; i++){
          if(res.data[i].factor !== null){
            res.data[i].factors = []
            const list = res.data[i].factor.split(',')
            for(let j = 0 ; j<list.length ; j++){
              if(list[j] == 1){
                res.data[i].factors.push("有水")
              }else if(list[j] == 2){
                res.data[i].factors.push("有电")
              }else if(list[j] == 3){
                res.data[i].factors.push("无水")
              }else if(list[j] == 4){
                res.data[i].factors.push("无水")
              }else if(list[j] == 5){
                res.data[i].factors.push("有新能源车充电桩")
              }else if(list[j] == 6){
                res.data[i].factors.push("有卫生间")
              }else{
                res.data[i].factors.push("其他条件")
              }
            }
          }
        }
        this.setData({
          campConditionList: res.data
        })
      }
      // console.log(res, "营位列表");
    })
  },
  // 营位
  toCampType() {
    this.setData({
      campCondotionStatus: true,
    })
  },
  //营位选择数据
  campConditionList(e) {
    console.log('e.detail:',e.detail);
    this.setData({
      campConditionDatas: e.detail,
      touristConditionData: e.detail
    })
    // console.log(e);
    const campSite = this.data.campConditionDatas
    const index = campSite.length - 1
    // 添加
    //........
    const result = campSite.map(item => {
      item.factor = item.factorId?.join(",")
      item.campid = this.data.campId
      return item
    })
    const object = result[index]
    var isEmpty = true;
    Object.keys(object).forEach(function (x) {
      if (object[x] === null || object[x] === "") {
        isEmpty = false;
      }
    });
    if (isEmpty) {
      http.addCampsite([result[index]]).then((res) => {
        // console.log(res);
        if (res.code === 200) {
          wx.showToast({
            title: '提交成功',
            icon: 'none',
            duration: 1500
          })
          this.getCampCampsite()
        }
      })
    }

  },
  // 删除营位
  delCondition(event) {
    Dialog.confirm({
        title: '营位删除',
        message: '确定删除该营位吗？',
      })
      .then(() => {
        console.log(event.currentTarget)
        http.delCampsite(event.currentTarget.dataset.id).then(res => {
          console.log(res)
          if (res.code == 200) {
            this.getCampCampsite()
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  
  // 获取项目
  getProject() {
    api.getProject(this.data.campId).then(res => {
      if (res.code == 200 && res.data) {
        this.setData({
          projectList: res.data
        })
      }
      // console.log(res, "项目列表");
    })
  },
  // 添加项目组件
  addProject() {
    // console.log(123)
    this.setData({
      projectStatus: true,
    })
  },
  //项目数据
  projectList(e) {
    this.setData({
      projectDatas: e.detail,
    })
    console.log("E:",e)
    const project = this.data.projectDatas
    const index = project.length - 1
    // 添加请求
    const object = project[index]
    var isEmpty = true;
    Object.keys(object).forEach(function (x) {
      if (object[x] === null || object[x] === "") {
        isEmpty = false;
      }
    });
    if (isEmpty) {
      http.addProject({
        ...project[index],
        campid: this.data.campId
      }).then((res) => {
        console.log(res);
        if (res.code === 200) {
          wx.showToast({
            title: '提交成功，待审核',
            icon: 'none',
            duration: 1500
          })

          this.getProject()
        }
      })
    }

  },
  // 删除项目
  delProject(event) {
    Dialog.confirm({
        title: '项目删除',
        message: '确定删除该项目吗？',
      })
      .then(() => {
        http.delProject(event.currentTarget.dataset.id).then(res => {
          if (res.code == 200) {
            this.getProject()
          }
        })
      })
      .catch(() => {
        // on cancel
      });

  },
  // 获取活动
  getActivity() {
    api.getActivity(this.data.campId).then(res => {
      if (res.code == 200 && res.data) {
        this.setData({
          activityList: res.data
        })
      }
      // console.log(res, "活动列表");
    })
  },
  // 添加活动组件
  addActivity() {
    // console.log(123);
    this.setData({
      campActivityStatus: true,
    })
  },
  //活动数据
  activityList(e) {
    this.setData({
      activityDatas: e.detail,
      // touristConditionData: e.detail
    })
    console.log(e);
    const activity = this.data.activityDatas
    const index = activity.length - 1
    // 添加请求
    const object = activity[index]
    var isEmpty = true;
    Object.keys(object).forEach(function (x) {
      if (object[x] === null || object[x] === "") {
        isEmpty = false;
      }
    });
    if (isEmpty) {
      http.addActivity({
        ...activity[index],
        campid: this.data.campId
      }).then((res) => {
        console.log(res);
        if (res.code === 200) {
          wx.showToast({
            title: '提交成功，待审核',
            icon: 'none',
            duration: 1500
          })
          this.getActivity()
        }
      })
    }
    
  },
  // 删除活动
  delActivity(event) {
    Dialog.confirm({
        title: '活动删除',
        message: '确定删除该活动吗？',
      })
      .then(() => {
        http.delActivity(event.currentTarget.dataset.id).then(res => {
          if (res.code == 200) {
            this.getActivity()
          }
        })
      })
      .catch(() => {
        // on cancel
      });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.campId);
    this.setData({
      campId: options.campId
    })

  },
  onShow() {
    this.getScenicInfo()
    this.getCampCampsite()
    this.getProject()
    this.getActivity()
  }


})